package com.ly.hotel.view.fragments.vod;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Episodepart;
import com.ly.hotel.model.Vodpart;
import com.ly.hotel.util.ImageUtil;
import com.ly.hotel.util.LanguageUtil;

import java.util.List;

public class  VodEpisodeAdapter extends BaseAdapter {
	Context mContext;
	LayoutInflater mInflater;
	private List<Episodepart> vodHeadList;
	private int rod;
	private String tmp[];


	public VodEpisodeAdapter(Context c, List<Episodepart> episodeList) {
		this.vodHeadList = episodeList;
		mContext = c;
		mInflater = LayoutInflater.from(mContext);
		tmp = MyApplication.getHostIp().split(":");
		rod = mContext.getResources().getDimensionPixelSize(R.dimen.px9);
	}
	
	@Override
	public int getCount() {
		return vodHeadList.size();
	}

	@Override
	public Object getItem(int postion) {
		return vodHeadList.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		return postion;
	}
	
	
	@Override
	public View getView(int postion, View contentView, ViewGroup arg2) {
		// View v = new View(mContext);
		String imgPath;
		ViewHolder holder;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = mInflater.inflate(R.layout.vod_gridview_item, null);
			holder.mImg = (ImageView) contentView.findViewById(R.id.mVodImage);
			holder.mTxt= (TextView) contentView.findViewById(R.id.mVodtext);
			contentView.setTag(holder);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		Vodpart vpart = vodHeadList.get(postion).getmovieType();
		if(tmp.length == 2 && !vodHeadList.get(postion).getServerPort().equals("0")){
			imgPath = "http://" + tmp[0] + ":" + vodHeadList.get(postion).getServerPort() + "/" + vodHeadList.get(postion).getPath();
		}else{
			imgPath = MyApplication.getHostResUrl() + vodHeadList.get(postion).getPath();
		}
		ImageUtil.displayImage_Round(mContext, imgPath, holder.mImg,rod);
		holder.mTxt.setText(LanguageUtil.getTextFromLocalLanguage(vpart.getVodpartname().replace("TV_", "")));
		return contentView;
	}
	
	static class ViewHolder {
		ImageView mImg;
		TextView mTxt;
	}
}
